import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_SERVER_URL_SSO,
  withCredentials: false, // send cookies when cross-domain requests
  // crossDomain: true,
  timeout: 15000 // request timeout
})

// request interceptor 请求拦截
service.interceptors.request.use(
  config => {
    if (store.getters.mfAgent) {
      config.headers['Mfagentid'] = store.getters.mfAgent.agentId;
      config.headers['Mfuuid'] = store.getters.mfAgent.uuid;
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor 响应拦截
service.interceptors.response.use(
  response => {
    const res = response.data
    return res;
  },
  error => {
    Message({
      message: error.message ,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
